package com.jiantong.work.impl.service;

import com.google.common.base.Throwables;
import com.google.common.collect.Maps;
import com.jiantong.work.enums.DocumentsDeleteFlag;
import com.jiantong.work.impl.dao.BreedTransferRecordDao;
import com.jiantong.work.model.BreedTransferRecord;
import com.jiantong.work.service.BreedTransferRecordReadService;
import io.terminus.boot.rpc.common.annotation.RpcProvider;
import io.terminus.common.model.PageInfo;
import io.terminus.common.model.Paging;
import io.terminus.common.model.Response;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;

/**
 * Created by mz on 14/8/17.
 * 转群读服务的实现
 */
@Slf4j
@Service
@RpcProvider
public class BreedTransferRecordReadServiceImpl implements BreedTransferRecordReadService{

    private final BreedTransferRecordDao breedTransferRecordDao;

    @Autowired
    public BreedTransferRecordReadServiceImpl(BreedTransferRecordDao breedTransferRecordDao) {
        this.breedTransferRecordDao = breedTransferRecordDao;
    }

    /**
     * 根据ID查询该转群的具体信息
     *
     * @param id 单据主键ID
     * @return BreedTransferRecord
     */
    @Override
    public Response<BreedTransferRecord> findById(Long id) {
        try{
            return Response.ok(breedTransferRecordDao.findById(id));
        }catch (Exception e){
            log.error("failed to find breed transfer record by id , case = {}", Throwables.getStackTraceAsString(e));
            return Response.fail("failed to find breed transfer record by id");
        }
    }

    /**
     * 养殖端分页查询转群的记录
     *
     * @param pageNo         页码
     * @param pageSize       每页条数
     * @param breedFactoryId 养殖场id
     * @param fromStableId   从栏舍id
     * @param toStableId     到栏舍id
     * @param start          从时间
     * @param end            到时间
     * @param status         状态
     * @return Paging<BreedTransferRecord>
     */
    @Override
    public Response<Paging<BreedTransferRecord>> paging(Integer pageNo, Integer pageSize,
                                                        Long breedFactoryId,
                                                        Long fromStableId, Long toStableId,
                                                        Date start, Date end, Integer status) {
        try{
            PageInfo pageInfo = new PageInfo(pageNo,pageSize);
            Map<String , Object> criteria = Maps.newHashMap();

            criteria.put("breedFactoryId",breedFactoryId);

            if (fromStableId != null) criteria.put("fromStableId",fromStableId);

            if (toStableId != null) criteria.put("toStableId",toStableId);

            if (start != null) criteria.put("start",start);

            if (end != null) criteria.put("end",end);

            if (status != null) criteria.put("status",status);

            criteria.put("deleteFlag", DocumentsDeleteFlag.NORMAL.value());

            return Response.ok(breedTransferRecordDao.paging(pageInfo.getOffset(),pageInfo.getLimit(),criteria));
        }catch (Exception e){
            log.error("failed to paging breed transfer record , case = {}",Throwables.getStackTraceAsString(e));
            return Response.fail("failed to paging breed transfer record");
        }
    }

    /**
     * 查询单据当前状态
     *
     * @param id 单据id
     * @return 状态
     */
    @Override
    public Response<Map<String, Integer>> findStatusById(Long id) {
        try{
            Integer status = breedTransferRecordDao.findStatusById(id);
            if(status == null){
                log.error("BreedTransferRecord status(id={}) is not found", id);
                return Response.fail("BreedTransferRecord status.not.found");
            }
            Map<String ,Integer> map = Maps.newHashMap();
            map.put("status",status);
            return Response.ok(map);
        }catch (Exception e){
            log.error("failed to find BreedTransferRecord status(id={}), cause:{}", id, Throwables.getStackTraceAsString(e));
            return Response.fail("BreedTransferRecord status.find.fail");
        }
    }
}
